package com.dingwei.superiordiary.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.mvp.presenter.LoginPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.AppManager;
import com.lihao.baselibrary.utils.L;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by lihao on 2018/1/11.
 */

public class LoginActivity extends BaseActivity implements Contract.LoginView {
    LoginPresenter loginPresenter = new LoginPresenter(this);
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.et_phone)
    EditText etPhone;
    @Bind(R.id.et_password)
    EditText etPassword;
    @Bind(R.id.tv_forget_pass)
    TextView tvForgetPass;
    @Bind(R.id.btn_login)
    Button btnLogin;
    @Bind(R.id.btn_register)
    Button btnRegister;
    @Bind(R.id.img_WeiBo)
    ImageView imgWeiBo;
    @Bind(R.id.img_Wechat)
    ImageView imgWechat;
    @Bind(R.id.img_QQ)
    ImageView imgQQ;

    private UMShareAPI mShareAPI;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        tvTitle.setText("登录");
        mShareAPI = UMShareAPI.get(context);
        UMShareConfig config = new UMShareConfig();
        config.isNeedAuthOnGetUserInfo(true);
        UMShareAPI.get(LoginActivity.this).setShareConfig(config);
    }

    @Override
    protected void initListener() {

    }

    @OnClick({R.id.img_back, R.id.tv_forget_pass, R.id.btn_login, R.id.btn_register, R.id.img_WeiBo, R.id.img_Wechat, R.id.img_QQ})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_forget_pass:
                intent.setClass(BaseActivity.context, ForGetPasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_login:
                loginPresenter.login(etPhone.getText().toString(), etPassword.getText().toString());
                break;
            case R.id.btn_register:
                intent.setClass(BaseActivity.context, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.img_WeiBo:
                mShareAPI.getPlatformInfo(LoginActivity.this, SHARE_MEDIA.SINA, umAuthListener);
                break;
            case R.id.img_Wechat:
                mShareAPI.getPlatformInfo(LoginActivity.this, SHARE_MEDIA.WEIXIN, umAuthListener);
                break;
            case R.id.img_QQ:
                mShareAPI.getPlatformInfo(LoginActivity.this, SHARE_MEDIA.QQ, umAuthListener);
                break;
            default:
                break;
        }
    }

    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            //授权开始的回调
            dialog.show();
        }

        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            String type = "qq";
            switch (platform.toString()) {
                case "QQ":
                    type = "2";
                    break;
                case "WEIXIN":
                    type = "1";
                    break;
                case "SINA":
                    type = "wb";
                    break;
            }
            L.e(platform.toString());
            loginPresenter.WXlogin(type, data.get("uid"), data.get("name"));
            dialog.dismiss();

        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText(getApplicationContext(), "授权失败", Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText(getApplicationContext(), "取消授权", Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }
    };

    @Override
    public void loginSuccess() {
        AppManager.getAppManager().finishActivity();
    }

    @Override
    public void bindPhone(String name, String type, String code) {
        intent = new Intent(BaseActivity.context, BindPhoneActivity.class);
        intent.putExtra("frominfo", name);
        intent.putExtra("fromtype", type);
        intent.putExtra("fromuuid", code);
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
